package org.goobs.threading;

public class Semaphore {

	private int value;
	
	public Semaphore(int initialValue){
		this.value = initialValue;
	}
	
	public synchronized void P(){
		while(value == 0){
			try {
				this.wait();
			} catch (InterruptedException e) {
				throw new IllegalStateException("Semaphore interrupted on P()");
			}
		}
		value--;
	}
	
	public synchronized void V(){
		value++;
		this.notifyAll();
	}

}
